package org.dromara.wms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.bo.WmsProductBo;
import org.dromara.wms.domain.vo.WmsProductVo;
import org.dromara.wms.domain.WmsProduct;
import org.dromara.wms.mapper.WmsProductMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品管理Service业务层处理
 *
 * @author wenrui
 * @date 2024-03-20
 */
@RequiredArgsConstructor
@Service
public class WmsProductServiceImpl extends BaseService {

  private final WmsProductMapper baseMapper;

  /**
   * 查询产品管理
   */
  public WmsProductVo queryById(Long id) {
    return baseMapper.selectVoById(id);
  }

  /**
   * 查询产品管理列表
   */
  public TableDataInfo<WmsProductVo> queryPageList(WmsProductBo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<WmsProduct> lqw = buildQueryWrapper(bo);
    Page<WmsProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  /**
   * 查询产品管理列表
   */
  public List<WmsProductVo> queryList(WmsProductBo bo) {
    LambdaQueryWrapper<WmsProduct> lqw = buildQueryWrapper(bo);
    return baseMapper.selectVoList(lqw);
  }

  private LambdaQueryWrapper<WmsProduct> buildQueryWrapper(WmsProductBo bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<WmsProduct> lqw = Wrappers.lambdaQuery();
    lqw.eq(StringUtils.isNotBlank(bo.getSku()), WmsProduct::getSku, bo.getSku());
    lqw.like(StringUtils.isNotBlank(bo.getName()), WmsProduct::getName, bo.getName());
    lqw.like(StringUtils.isNotBlank(bo.getTreeValue()), WmsProduct::getTreeValue, bo.getTreeValue());
    lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
        WmsProduct::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
    return lqw;
  }

  /**
   * 新增产品管理
   */
  public Boolean insertByBo(WmsProductBo bo) {
    WmsProduct add = MapstructUtils.convert(bo, WmsProduct.class);
    validEntityBeforeSave(add);
    boolean flag = baseMapper.insert(add) > 0;
    if (flag) {
      bo.setId(add.getId());
    }
    return flag;
  }

  /**
   * 修改产品管理
   */
  public Boolean updateByBo(WmsProductBo bo) {
    WmsProduct update = MapstructUtils.convert(bo, WmsProduct.class);
    validEntityBeforeSave(update);
    return baseMapper.updateById(update) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validEntityBeforeSave(WmsProduct entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除产品管理
   */
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return baseMapper.deleteBatchIds(ids) > 0;
  }
}
